// Mock数据文件，用于开发环境提供模拟数据

// 模拟任务数据
export const mockTasks = [
  {
    id: '1',
    title: '完善个人信息',
    description: '请完善您的个人信息，包括头像、昵称、性别等基本资料。完成后可获得10积分奖励。',
    reward_points: 10,
    status: 'pending',
    category: 'newbie',
    participant_count: 2541,
    completion_rate: 89,
    created_at: Date.now() - 86400000,
    updated_at: Date.now() - 86400000,
    start_time: Date.now(),
    end_time: Date.now() + 30 * 24 * 60 * 60 * 1000,
    is_claimed: false,
    is_submitted: false,
    is_approved: false,
    is_rejected: false,
    tags: ['新手任务', '简单', '必做'],
    steps: [
      '点击个人中心',
      '选择编辑资料',
      '上传头像',
      '填写昵称和性别',
      '保存信息'
    ],
    requirements: [
      '头像必须真实清晰',
      '昵称不得包含违规内容',
      '必须填写真实性别'
    ],
    faqs: [
      { question: '完善信息需要多久？', answer: '通常只需2-3分钟即可完成。' },
      { question: '修改信息是否会影响已获得的积分？', answer: '不会，积分一旦获得不会因为修改信息而扣除。' }
    ]
  },
  {
    id: '2',
    title: '每日签到',
    description: '连续签到7天可获得额外奖励，坚持签到获取更多积分！每日签到可获得5积分，连续签到第7天可额外获得20积分。',
    reward_points: 5,
    status: 'pending',
    category: 'daily',
    participant_count: 12540,
    completion_rate: 78,
    created_at: Date.now() - 172800000,
    updated_at: Date.now() - 172800000,
    start_time: Date.now(),
    end_time: Date.now() + 1 * 24 * 60 * 60 * 1000,
    is_claimed: false,
    is_submitted: false,
    is_approved: false,
    is_rejected: false,
    tags: ['每日任务', '简单', '每日'],
    steps: [
      '点击签到按钮',
      '确认签到'
    ],
    requirements: [
      '每天只能签到一次',
      '必须在当天24点前完成签到'
    ],
    faqs: [
      { question: '忘记签到了怎么办？', answer: '连续签到天数会重置，但仍然可以继续签到获得基础积分。' },
      { question: '签到后积分什么时候到账？', answer: '签到成功后积分立即到账。' }
    ]
  },
  {
    id: '3',
    title: '分享给好友',
    description: '将任务平台分享给3个好友，并引导他们注册成功，完成后可获得30积分奖励。',
    reward_points: 30,
    status: 'pending',
    category: 'share',
    participant_count: 8672,
    completion_rate: 65,
    created_at: Date.now() - 259200000,
    updated_at: Date.now() - 259200000,
    start_time: Date.now(),
    end_time: Date.now() + 7 * 24 * 60 * 60 * 1000,
    is_claimed: false,
    is_submitted: false,
    is_approved: false,
    is_rejected: false,
    tags: ['分享任务', '奖励高'],
    steps: [
      '点击分享按钮',
      '选择分享方式',
      '发送给好友',
      '引导好友注册'
    ],
    requirements: [
      '好友必须通过您的分享链接注册',
      '需要至少3个好友成功注册',
      '任务有效期为7天'
    ],
    faqs: [
      { question: '如何确认好友是否通过我的链接注册？', answer: '系统会自动记录邀请关系，您可以在任务详情中查看进度。' },
      { question: '好友注册后多久可以获得积分？', answer: '好友注册成功后积分会在24小时内发放。' }
    ]
  },
  {
    id: '4',
    title: '产品体验反馈',
    description: '体验我们的产品功能，并填写详细的反馈问卷，完成后可获得20积分奖励。',
    reward_points: 20,
    status: 'pending',
    category: 'survey',
    participant_count: 5432,
    completion_rate: 72,
    created_at: Date.now() - 345600000,
    updated_at: Date.now() - 345600000,
    start_time: Date.now(),
    end_time: Date.now() + 14 * 24 * 60 * 60 * 1000,
    is_claimed: false,
    is_submitted: false,
    is_approved: false,
    is_rejected: false,
    tags: ['调查任务', '反馈'],
    steps: [
      '体验产品核心功能',
      '填写反馈问卷',
      '提交问卷'
    ],
    requirements: [
      '问卷必须填写完整',
      '反馈内容必须真实有效',
      '不得提交恶意或无意义内容'
    ],
    faqs: [
      { question: '问卷需要填写多长时间？', answer: '通常需要10-15分钟完成。' },
      { question: '我的反馈会被保密吗？', answer: '是的，所有用户反馈都会被严格保密。' }
    ]
  },
  {
    id: '5',
    title: '下载并体验应用',
    description: '下载我们的移动端应用并完成注册，体验至少3个核心功能，完成后可获得50积分奖励。',
    reward_points: 50,
    status: 'pending',
    category: 'app',
    participant_count: 3210,
    completion_rate: 58,
    created_at: Date.now() - 432000000,
    updated_at: Date.now() - 432000000,
    start_time: Date.now(),
    end_time: Date.now() - 1, // 已过期
    is_claimed: false,
    is_submitted: false,
    is_approved: false,
    is_rejected: false,
    tags: ['应用下载', '高奖励'],
    steps: [
      '扫描二维码下载应用',
      '注册账号',
      '完成新手引导',
      '体验至少3个核心功能'
    ],
    requirements: [
      '必须通过指定链接下载',
      '必须完成注册',
      '必须体验至少3个核心功能',
      '任务有效期为3天'
    ],
    faqs: [
      { question: '如何证明我已体验了3个功能？', answer: '系统会自动记录您的操作行为。' },
      { question: '应用下载需要多少流量？', answer: '应用大小约为50MB，请确保网络畅通。' }
    ]
  }
];

// 模拟用户数据
export const mockUser = {
  id: 'user123',
  openid: 'wx123456789',
  nickname: '测试用户',
  avatar: 'https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLOl7jibg9y9JcQK6oOJ0lCwVbLJZ3X9bXg/132',
  gender: 1,
  phone: '138****8888',
  email: '',
  points: 125,
  level: 3,
  created_at: Date.now() - 7 * 24 * 60 * 60 * 1000,
  last_login_at: Date.now() - 3600000,
  tasks_completed: 15,
  tasks_pending: 3,
  tasks_rejected: 1
};

// 模拟积分记录
export const mockPointsRecords = [
  {
    id: '1',
    type: 'task_completed',
    points: 10,
    description: '完成任务：完善个人信息',
    created_at: Date.now() - 3600000,
    status: 'success'
  },
  {
    id: '2',
    type: 'daily_checkin',
    points: 5,
    description: '每日签到',
    created_at: Date.now() - 7200000,
    status: 'success'
  },
  {
    id: '3',
    type: 'task_completed',
    points: 20,
    description: '完成任务：产品体验反馈',
    created_at: Date.now() - 86400000,
    status: 'success'
  },
  {
    id: '4',
    type: 'task_rejected',
    points: -15,
    description: '任务被拒绝：分享给好友',
    created_at: Date.now() - 172800000,
    status: 'failed'
  },
  {
    id: '5',
    type: 'daily_checkin',
    points: 5,
    description: '每日签到',
    created_at: Date.now() - 176400000,
    status: 'success'
  }
];

// 模拟排行榜数据
export const mockRankingList = [
  {
    rank: 1,
    user_id: 'user001',
    nickname: '积分王者',
    avatar: 'https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLOl7jibg9y9JcQK6oOJ0lCwVbLJZ3X9bXg/132',
    points: 2580,
    is_self: false
  },
  {
    rank: 2,
    user_id: 'user002',
    nickname: '任务达人',
    avatar: 'https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLOl7jibg9y9JcQK6oOJ0lCwVbLJZ3X9bXg/132',
    points: 2450,
    is_self: false
  },
  {
    rank: 3,
    user_id: 'user003',
    nickname: '新手逆袭',
    avatar: 'https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLOl7jibg9y9JcQK6oOJ0lCwVbLJZ3X9bXg/132',
    points: 2130,
    is_self: false
  },
  {
    rank: 15,
    user_id: 'user123',
    nickname: '测试用户',
    avatar: 'https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLOl7jibg9y9JcQK6oOJ0lCwVbLJZ3X9bXg/132',
    points: 125,
    is_self: true
  }
];

// 模拟任务分类
export const mockCategories = [
  { id: 'all', name: '全部任务', icon: 'list' },
  { id: 'newbie', name: '新手任务', icon: 'newspaper' },
  { id: 'daily', name: '每日任务', icon: 'calendar' },
  { id: 'share', name: '分享任务', icon: 'share' },
  { id: 'survey', name: '调查任务', icon: 'chatbox' },
  { id: 'app', name: '应用下载', icon: 'download' }
];

// Mock响应延迟
const delay = (ms = 500) => new Promise(resolve => setTimeout(resolve, ms));

// 模拟API响应
export const mockApi = {
  // 获取任务列表
  async getTasks(params = {}) {
    await delay();
    let tasks = [...mockTasks];
    
    // 根据类型筛选
    if (params.type && params.type !== 'all') {
      if (params.type === 'recommend') {
        tasks = tasks.filter(task => task.tags.includes('必做') || task.reward_points > 20);
      } else if (params.type === 'hot') {
        tasks = tasks.sort((a, b) => b.participant_count - a.participant_count);
      } else if (params.type === 'newest') {
        tasks = tasks.sort((a, b) => b.created_at - a.created_at);
      } else if (params.type === 'pending') {
        tasks = tasks.filter(task => task.status === 'pending' && task.category === 'newbie');
      } else {
        tasks = tasks.filter(task => task.category === params.type);
      }
    }
    
    // 分页
    const page = params.page || 1;
    const pageSize = params.pageSize || 10;
    const startIndex = (page - 1) * pageSize;
    const endIndex = startIndex + pageSize;
    const paginatedTasks = tasks.slice(startIndex, endIndex);
    
    return {
      code: 200,
      success: true,
      data: paginatedTasks,
      total: tasks.length,
      page,
      pageSize,
      totalPages: Math.ceil(tasks.length / pageSize)
    };
  },
  
  // 获取任务详情
  async getTaskDetail(taskId) {
    await delay();
    const task = mockTasks.find(task => task.id === taskId);
    
    if (!task) {
      return {
        code: 404,
        success: false,
        message: '任务不存在'
      };
    }
    
    return {
      code: 200,
      success: true,
      data: task
    };
  },
  
  // 领取任务
  async claimTask(taskId) {
    await delay();
    const task = mockTasks.find(task => task.id === taskId);
    
    if (!task) {
      return {
        code: 404,
        success: false,
        message: '任务不存在'
      };
    }
    
    if (task.end_time < Date.now()) {
      return {
        code: 400,
        success: false,
        message: '任务已过期'
      };
    }
    
    // 模拟领取成功
    task.is_claimed = true;
    
    return {
      code: 200,
      success: true,
      message: '领取成功',
      data: { task_id: taskId }
    };
  },
  
  // 提交任务
  async submitTask(taskId, submitData) {
    await delay();
    const task = mockTasks.find(task => task.id === taskId);
    
    if (!task) {
      return {
        code: 404,
        success: false,
        message: '任务不存在'
      };
    }
    
    if (!task.is_claimed) {
      return {
        code: 400,
        success: false,
        message: '请先领取任务'
      };
    }
    
    // 模拟提交成功
    task.is_submitted = true;
    
    return {
      code: 200,
      success: true,
      message: '提交成功，请等待审核',
      data: { task_id: taskId, submit_id: Date.now().toString() }
    };
  },
  
  // 获取用户信息
  async getUserInfo() {
    await delay();
    return {
      code: 200,
      success: true,
      data: mockUser
    };
  },
  
  // 获取积分记录
  async getPointsRecords(params = {}) {
    await delay();
    let records = [...mockPointsRecords];
    
    // 分页
    const page = params.page || 1;
    const pageSize = params.pageSize || 10;
    const startIndex = (page - 1) * pageSize;
    const endIndex = startIndex + pageSize;
    const paginatedRecords = records.slice(startIndex, endIndex);
    
    return {
      code: 200,
      success: true,
      data: paginatedRecords,
      total: records.length,
      page,
      pageSize
    };
  },
  
  // 获取排行榜
  async getRankingList() {
    await delay();
    return {
      code: 200,
      success: true,
      data: mockRankingList
    };
  },
  
  // 微信登录
  async wxLogin(code) {
    await delay(1000); // 模拟网络延迟
    return {
      code: 200,
      success: true,
      data: {
        token: 'mock_token_' + Date.now(),
        userInfo: mockUser,
        expires_in: 7200
      }
    };
  },
  
  // 手机号登录
  async phoneLogin(phone, code) {
    await delay(1000);
    return {
      code: 200,
      success: true,
      data: {
        token: 'mock_token_' + Date.now(),
        userInfo: mockUser,
        expires_in: 7200
      }
    };
  }
};

// 导出模拟数据和API
export default {
  mockTasks,
  mockUser,
  mockPointsRecords,
  mockRankingList,
  mockCategories,
  mockApi
};